﻿Imports Ecodex.RestAPI
Imports System.Text
Imports System.ServiceModel
Imports System.Configuration


Public Class frmGetDocuments
    Private Sub btnListDocuments_Click(sender As System.Object, e As System.EventArgs) Handles btnListDocuments.Click
        Try


            Dim servicio = ConfigurationManager.AppSettings.GetValues("urlServicio")
            Dim url As Uri = New Uri(servicio(0))
            Dim jsonApi As RestApi = New RestApi(url)
            Dim tokenService = jsonApi.GetToken(txtRFC.Text, Utilerias.Integrator_ID)
            Dim list As List(Of Models.DocumentoInfo) = jsonApi.GetDocumentos(tokenService)
            Dim sb As StringBuilder = New StringBuilder()
            For Each cfdi As Models.DocumentoInfo In list
                sb.AppendLine(String.Format("{0} {1} {2}", cfdi.TipoDocumento, cfdi.UUID, cfdi.FechaTimbrado))
            Next
            txtListDocuments.Text = sb.ToString
        Catch appFault As FaultException(Of Exceptions.FallaAplicacion)
            Using errorForm = New frmError(TranslateFault.ToAppError(appFault.Detail))
                errorForm.ShowDialog()

            End Using
        Catch validationFault As FaultException(Of Exceptions.FallaValidacion)
            Using errorForm = New frmError(TranslateFault.ToValidationError(validationFault.Detail))
                errorForm.ShowDialog()
            End Using
        Catch sessionFault As FaultException(Of Exceptions.FallaSesion)
            Using errorForm = New frmError(TranslateFault.ToSessionError(sessionFault.Detail))
                errorForm.ShowDialog()
            End Using
        Catch ex As Exception
            ShowUnknownError(ex.Message)
        End Try
    End Sub

    Public Sub ShowUnknownError(ByVal message As String)
        MessageBox.Show([String].Format("Error: {0}", message), "Ecodex Cancela Documento")
    End Sub


End Class